package com.quanyue.mechanics.user.controller;

import com.quanyue.auto.util.AssertUtils;
import com.quanyue.auto.util.ServiceResult;
import com.quanyue.auto.web.Principal;
import com.quanyue.auto.web.Scope;
import com.quanyue.mechanics.domain.constant.UserRole;
import com.quanyue.mechanics.domain.entity.Manager;
import com.quanyue.mechanics.user.service.ManagerService;
import com.quanyue.mechanics.user.type.SignIn;
import com.quanyue.mechanics.user.type.UpdatePassword;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

@Api(tags = "管理员")
@Scope(role = UserRole.MANAGER)
@RestController
@RequestMapping("/api")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    @ApiOperation(value = "登录")
    @PostMapping(value = "/manager/login", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Integer> login(@RequestBody SignIn login) {
        Manager manager = managerService.findByPassword(login.getUsername(), login.getPassword());
        AssertUtils.notNull(manager, "用户名密码错误");
        return ServiceResult.success(manager.getId());
    }

    @ApiOperation(value = "生成token")
    @PostMapping(value = "/manager/token", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <String> login(@RequestParam("id") Integer id) {
        return ServiceResult.success(Base64.getEncoder().encodeToString(("manager," + id).getBytes(StandardCharsets.UTF_8)));
    }

    @ApiOperation(value = "列表")
    @GetMapping(value = "/manager/list", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <Manager>> list(Principal principal) {
        return ServiceResult.success(managerService.findAll(principal.getId()));
    }

    @ApiOperation(value = "个人信息")
    @GetMapping(value = "/manager/session", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Manager> session(@RequestParam(value = "id", required = false) Integer id) {
        return ServiceResult.success(managerService.sessionManager(id));
    }

    @ApiOperation(value = "个人信息")
    @GetMapping(value = "/manager/load", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <List <Manager>> load(@RequestParam(value = "id", required = false) Integer id, @ApiIgnore Principal manager) {
        return ServiceResult.success(managerService.findById(id == null ? manager.getId() : id));
    }

    @ApiOperation(value = "增加")
    @PostMapping(value = "/manager/add", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Manager> add(@RequestBody Manager manager) {
        return ServiceResult.success(managerService.add(manager));
    }

    @ApiOperation(value = "修改")
    @PutMapping(value = "/manager/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> update(@RequestBody Manager manager) {
        managerService.update(manager);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "删除")
    @DeleteMapping(value = "/manager/remove", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> remove(@RequestParam("id") Integer id) {
        managerService.remove(id);
        return ServiceResult.SUCCESS;
    }

    @ApiOperation(value = "修改密码")
    @PutMapping(value = "/manager/update/password", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public ServiceResult <Boolean> update(@RequestBody UpdatePassword password, @ApiIgnore Principal manager) {
        managerService.updatePassword(manager.getId(), password.getCurrent(), password.getPassword());
        return ServiceResult.SUCCESS;
    }
}
